package jp.sourceforge.akjrcp.util;

import jp.sourceforge.akjrcp.ffmpeg.FFMpegUtils;
import jp.sourceforge.akjrcp.ui.DirectryBasedExecuterListener;

import org.eclipse.ui.PlatformUI;


import jp.sourceforge.akjrcp.akj_betas.Messages;
public class FFMpegShowStatusProcessPrinter extends ShowStatusProcessPrinter{

	public void printOutput(String output) {
		if(!FFMpegUtils.isProgressString(output)){
			print(output);
		}
	}
	
	
	public void print(String string){
		log.append(string);
		//append infomation
		//System.out.println("**"+string);
		if(string.indexOf("ffmpeg: unrecognized option")!=-1){
			PlatformUI.getWorkbench().getDisplay().asyncExec(new StatusBarUtils.ShowStatusBar(Messages.getString("ffmpeg_info.faild")));
			log.append(Messages.getString("ffmpeg_error.unrecognized_option")+"\n");
			log.append(Messages.getString("ffmpeg_hint.update_ffmpeg")+"\n");
			log.append("\n");
			
			PlatformUI.getWorkbench().getDisplay().asyncExec(new StatusBarUtils.ShowStatusBar(Messages.getString("ffmpeg_info.faild")));
		}else if(string.indexOf(DirectryBasedExecuterListener.ERROR_NOTSUPPORTED_VIDEO)!=-1){
			log.append(Messages.getString("ffmpeg_error.unsupported")+"\n");
			log.append(Messages.getString("ffmpeg_hint.update_ffmpeg")+"\n");
			log.append(Messages.getString("ffmpeg_hint.install_avisynth")+"\n");
			log.append("\n");
			//show error
			PlatformUI.getWorkbench().getDisplay().asyncExec(new StatusBarUtils.ShowStatusBar(Messages.getString("ffmpeg_info.faild")));
		}else if(string.indexOf(DirectryBasedExecuterListener.ERROR_NOTSUPPORTED_SOMETHING)!=-1){
			log.append(Messages.getString("ffmpeg_error.unsupported_something")+"\n");
			log.append(Messages.getString("ffmpeg_hint.update_ffmpeg")+"\n");
			
			log.append("\n");
			//show error
			PlatformUI.getWorkbench().getDisplay().asyncExec(new StatusBarUtils.ShowStatusBar(Messages.getString("ffmpeg_info.faild")));
		}else if(string.indexOf(DirectryBasedExecuterListener.ERROR_COULD_NOT_OPEN)!=-1){
			log.append(Messages.getString("ffmpeg_error.could_not_open")+"\n");
			log.append(Messages.getString("ffmpeg_hint.could_not_open")+"\n");
			
			log.append("\n");
			//show error
			PlatformUI.getWorkbench().getDisplay().asyncExec(new StatusBarUtils.ShowStatusBar(Messages.getString("ffmpeg_info.faild")));
		}else if(string.indexOf("av_interleaved_write_frame():")!=-1){
			log.append(Messages.getString("ffmpeg_error.unsupported_copy_audio")+"\n");
			log.append(Messages.getString("ffmpeg_hint.unsupported_copy_audio")+"\n");
			
			log.append("\n");
			//show error
			PlatformUI.getWorkbench().getDisplay().asyncExec(new StatusBarUtils.ShowStatusBar(Messages.getString("ffmpeg_info.faild")));
		}else if(string.indexOf("no such file or directory")!=-1 && string.indexOf("?")!=-1){
			log.append(Messages.getString("ffmpeg_error.not_supported_file_name")+"\n");
			log.append(Messages.getString("ffmpeg_hint.not_supported_file_name")+"\n");
			PlatformUI.getWorkbench().getDisplay().asyncExec(new StatusBarUtils.ShowStatusBar(Messages.getString("ffmpeg_info.faild")));
			
			log.append("\n");
		}else if(string.indexOf("Cannot allocate temp picture, check pix fmt")!=-1 && string.indexOf("?")!=-1){
			log.append(Messages.getString("ffmpeg_error.maybe_lack_of_memory")+"\n");
			PlatformUI.getWorkbench().getDisplay().asyncExec(new StatusBarUtils.ShowStatusBar(Messages.getString("ffmpeg_info.faild")));
			
			log.append("\n");
		}else if(string.indexOf("PSP mode need one video and one audio stream")!=-1){
			log.append(Messages.getString("ffmpeg_error.psp_neeed_audio_track")+"\n");
			PlatformUI.getWorkbench().getDisplay().asyncExec(new StatusBarUtils.ShowStatusBar(Messages.getString("ffmpeg_info.faild")));
			
			log.append("\n");
		}else if(string.indexOf("faac: frame decoding failed: Invalid number of channels")!=-1){
			log.append(Messages.getString("ffmpeg_error.faac_frame_faild")+"\n");
			
			log.append("\n");
		}
	}
	
	public void printError(String error) {
		
		//if ffmpeg log.
		if(!FFMpegUtils.isProgressString(error)){
			print(error);
			
		}else{
		PlatformUI.getWorkbench().getDisplay().asyncExec(new StatusBarUtils.ShowStatusBar(error));
		if(error.indexOf("Lsize=")!=-1){ //print final report
			print(error);
			}
		}
		
		}
}
